#include "MoveManager.h"

#include <OGRE/OgreNode.h>
#include <OGRE/OgreFrameListener.h>

using namespace Viewer;

MoveManager::MoveManager(Ogre::Node* node, OIS::Keyboard* keyboard, OIS::JoyStick* js)
    : _node(node), _keyboard(keyboard), _joystick(js), _keyUp(0), _keyDown(0), _keyLeft(0), _keyRight(0)
{       }

MoveManager::~MoveManager()
{       }

bool MoveManager::keyPressed(const OIS::KeyEvent& arg)
{
    return true;
}

bool MoveManager::keyReleased(const OIS::KeyEvent& arg)
{
    return true;
}

bool MoveManager::mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
{
    return true;
}

bool MoveManager::mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
{
    return true;
}

bool MoveManager::mouseMoved(const OIS::MouseEvent& arg)
{
    return true;
}

bool MoveManager::axisMoved(const OIS::JoyStickEvent& arg, int axis)
{
    return true;
}

void MoveManager::nextFrame(const Ogre::FrameEvent& evt)
{
    if(_keyboard->isKeyDown(OIS::KC_UP))
        _node->translate(0, 0, -10, Ogre::Node::TS_LOCAL);
    if(_keyboard->isKeyDown(OIS::KC_LEFT))
        _node->rotate(Ogre::Vector3::UNIT_Y, Ogre::Radian(0.07));
    if(_keyboard->isKeyDown(OIS::KC_RIGHT))
        _node->rotate(Ogre::Vector3::UNIT_Y, Ogre::Radian(-0.07));
    
    if(_joystick) {
        double jsx = _joystick->getJoyStickState().mAxes.at(3).abs / 32000.0;
        double jsy = _joystick->getJoyStickState().mAxes.at(4).abs / 32000.0;
        if(jsx < -0.25 || jsx > 0.25)
            _node->rotate(Ogre::Vector3::UNIT_Y, Ogre::Radian(jsx * -0.1));
        if(jsy < -0.25 || jsy > 0.25)
            _node->translate(0, 0, jsy * 10, Ogre::Node::TS_LOCAL);
    }
}
